@file:JvmName("ToastUtilKTKt")

package com.example.common.tools

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.widget.Toast
import java.lang.Exception

fun Any.toastMain(context: Context, duration: Int = Toast.LENGTH_SHORT): MyToast {
    return MyToast(context, this.toString(), duration)
}

fun Any.toast(duration: Int = Toast.LENGTH_SHORT): Toast {
    try {
        var height = TooldUtils.getScreenHeight()
        var toast = Toast.makeText(TooldUtils.getApplication(), this.toString(), duration)
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, height/5)
        return toast.apply { show() }
    } catch (e: Exception) {
        return Toast(TooldUtils.getApplication())
    }
}


class MyToast(var context: Context, var text: CharSequence, var duration: Int) {

    companion object {
        private val handler = Handler(Looper.getMainLooper())
    }

    fun show() {
        handler.post {
            var toast = Toast.makeText(context, text, duration)
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.show()
        }
    }
}

